package edu.gatech.oad.OTRandom;
import edu.gatech.oad.OTConfig.*;

import java.util.Random;
import javax.swing.*;

/**
 * This is the EventController class.
 * This class implements a Random Event.
 * @author Aditya Desai
 * @version 1.0 11/1/11
 */

public class EventController {

	private Random generator = new Random();
	private int randNum1 = generator.nextInt(11);
	private int randNum2 = generator.nextInt(4);
	private int randNum3 = generator.nextInt(3) + 1;
	private int randNum4 = generator.nextInt(5);
	private int randNum5 = generator.nextInt(2);
	private int randNum6 = generator.nextInt(6);
	private InventoryController inventoryController = Wagon.getInventoryController();
	//private JourneyController journeyController = TrailPanel.getJourneyController();
	

	/**
	 * This is the riverEvent method
	 * This method generates a wagon flip if the wagon unsuccessfully crosses the river.
	 */
	
	public void riverEvent() {
		randNum1 = generator.nextInt(11);
		randNum2 = generator.nextInt(4);
		randNum4 = generator.nextInt(5);
		randNum5 = generator.nextInt(2);
		randNum6 = generator.nextInt(6);
		randNum3 = generator.nextInt(3) + 1;
		switch (randNum5) {
		case 0:
			switch (randNum2) {
			case 0:
				if (Wagon.getSettlerOne().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerOne().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerOne(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerOne().getName() + " has drowned!");
				}
				break;
			case 1:
				if (Wagon.getSettlerTwo().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerTwo().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerTwo().getName() + " has drowned!");
				}
				break;
			case 2:
				if (Wagon.getSettlerThree().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerThree().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerThree(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerThree().getName() + " has drowned!");
				}
				break;
			case 3:
				if (Wagon.getSettlerFour().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerFour().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerFour(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerFour().getName() + " has drowned!");
				}
				break;
			}
			break;
		case 1:
			switch (randNum6) {
			case 0:
				if (inventoryController.getWagonInventory().get(0).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! An ox was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 1:
				if (inventoryController.getWagonInventory().get(1).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(1).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! Food was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 2:
				if (inventoryController.getWagonInventory().get(2).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(2).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! Clothing was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 3:
				if (inventoryController.getWagonInventory().get(3).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(3).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A box of ammo was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 4:
				if (inventoryController.getWagonInventory().get(4).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(4).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A wheel was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 5:
				if (inventoryController.getWagonInventory().get(5).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(5).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! An axle was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			case 6:
				if (inventoryController.getWagonInventory().get(6).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(6).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon flipped during the river crossing! A tongue was lost from your wagon!");
				}
				else {
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
				break;
			}
		break; 
		}
	}

	/**
	 * This is the generateEvent Method
	 * This method generates a random event.
	 */

	public void generateEvent() {
		randNum1 = generator.nextInt(11);
		randNum2 = generator.nextInt(4);
		randNum4 = generator.nextInt(5);
		randNum5 = generator.nextInt(2);
		randNum6 = generator.nextInt(6);
		randNum3 = generator.nextInt(3) + 1;
		switch (randNum1) {

		//There is a storm
		case 0:
		//	for (int i = 0; i < randNum3; i++) {
				//journeyController.rest();
		//	}
			JOptionPane.showMessageDialog(null, "A Storm has hit! You should rest for " + randNum3 + " day(s) until it passes.");
			break;

		//A settler or the player is ill
		case 1:
			switch (randNum4) {
			case 0:
				if (Wagon.getSettlerOne().getHealth().equals(HealthEnum.HEALTHY)) {
					Wagon.setSettlerHealth(Wagon.getSettlerOne(), "ill");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerOne().getName()+ " is ill.");
				}
				break;
			case 1:
				if (Wagon.getSettlerTwo().getHealth().equals(HealthEnum.HEALTHY)) {
					Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "ill");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerTwo().getName()+ " is ill.");
				}
				break;
			case 2:
				if (Wagon.getSettlerThree().getHealth().equals(HealthEnum.HEALTHY)) {
					Wagon.setSettlerHealth(Wagon.getSettlerThree(), "ill");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerThree().getName()+ " is ill.");
				}
				break;
			case 3:
				if (Wagon.getPlayer().getHealth().equals(HealthEnum.HEALTHY)) {
					Wagon.setPlayerHealth(Wagon.getPlayer(), "ill");
					JOptionPane.showMessageDialog(null, Wagon.getPlayer().getName()+ " is ill.");
				}
				break;
			default:
				if (Wagon.getSettlerFour().getHealth().equals(HealthEnum.HEALTHY)) {
					Wagon.setSettlerHealth(Wagon.getSettlerFour(), "ill");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerFour().getName()+ " is ill.");
				}
				break;
			}
			break;

		//The Wagon is Damaged
		case 2:
			switch (randNum3) {
			case 1:
				if (inventoryController.getWagonInventory().get(4).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(4).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use a wheel from your inventory to repair the wagon!");
				}
				break;
			case 2:
				if (inventoryController.getWagonInventory().get(5).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(5).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use an axle from your inventory to repair the wagon!");
				}
				break;
			case 3:
				if (inventoryController.getWagonInventory().get(6).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(6).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon is damaged! You must use a tongue from your inventory to repair the wagon!");
				}
				break;
			}
			break;

		//The Player Dies
		case 3:
			Wagon.setPlayerHealth(Wagon.getPlayer(), "dead");
			JOptionPane.showMessageDialog(null, "You have died on the trail! The game is over!");
			System.exit(0);
			break;

		//A Settler Dies
		case 4:
			switch (randNum2) {
			case 0:
				if (Wagon.getSettlerOne().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerOne().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerOne(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerOne().getName() + " has died on the trail!");
				}
				break;
			case 1:
				if (Wagon.getSettlerTwo().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerTwo().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerTwo().getName() + " has died on the trail!");
				}
				break;
			case 2:
				if (Wagon.getSettlerThree().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerThree().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerThree(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerThree().getName() + " has died on the trail!");
				}
				break;
			default:
				if (Wagon.getSettlerFour().getHealth().equals(HealthEnum.HEALTHY) || Wagon.getSettlerFour().getHealth().equals(HealthEnum.ILL)) {
					Wagon.setSettlerHealth(Wagon.getSettlerFour(), "dead");
					JOptionPane.showMessageDialog(null, Wagon.getSettlerFour().getName() + " has died on the trail!");
				}
				break;
			}
			break;

		//Wagon robbed by UGA Student
		case 5:
			switch (randNum6) {
			case 0:
				if (inventoryController.getWagonInventory().get(0).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole an ox from your wagon!");
				}
				break;
			case 1:
				if (inventoryController.getWagonInventory().get(1).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(1).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole food from your wagon!");
				}
				break;
			case 2:
				if (inventoryController.getWagonInventory().get(2).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(2).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole clothing from your wagon!");
				}
				break;
			case 3:
				if (inventoryController.getWagonInventory().get(3).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(3).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a box of ammo from your wagon!");
				}
				break;
			case 4:
				if (inventoryController.getWagonInventory().get(4).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(4).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a wheel from your wagon!");
				}
				break;
			case 5:
				if (inventoryController.getWagonInventory().get(5).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(5).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole an axle from your wagon!");
				}
				break;
			case 6:
				if (inventoryController.getWagonInventory().get(6).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(6).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "A rogue UGA student has stolen a GT time machine and stole a tongue from your wagon!");
				}
				break;
			}
			break; 

		//Any ill member has recovered
		case 6:
			HealthEnum settler1Health = Wagon.getSettlerOne().getHealth();
			HealthEnum settler2Health = Wagon.getSettlerTwo().getHealth();
			HealthEnum settler3Health = Wagon.getSettlerThree().getHealth();
			HealthEnum settler4Health = Wagon.getSettlerFour().getHealth();
			HealthEnum playerHealth = Wagon.getPlayer().getHealth();
			if (settler1Health.equals(HealthEnum.ILL) || settler2Health.equals(HealthEnum.ILL) || settler3Health.equals(HealthEnum.ILL) || settler4Health.equals(HealthEnum.ILL) || playerHealth.equals(HealthEnum.ILL)) {
				if (settler1Health.equals(HealthEnum.ILL))
					Wagon.setSettlerHealth(Wagon.getSettlerOne(), "healthy");
				if (settler2Health.equals(HealthEnum.ILL))
					Wagon.setSettlerHealth(Wagon.getSettlerTwo(), "healthy");
				if (settler3Health.equals(HealthEnum.ILL))
					Wagon.setSettlerHealth(Wagon.getSettlerThree(), "healthy");
				if (settler4Health.equals(HealthEnum.ILL))
					Wagon.setSettlerHealth(Wagon.getSettlerFour(), "healthy");
				else
					Wagon.setPlayerHealth(Wagon.getPlayer(), "healthy");
				JOptionPane.showMessageDialog(null, "The ill members of your party have recovered!");
			}
			break;

		//Ox is dead
		case 7:
			if(inventoryController.getWagonInventory().get(0).getQuantity() == 0){
				JOptionPane.showMessageDialog(null, "All oxen have died on the trail! The game is over!");
				System.exit(0);
			}
			inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
			JOptionPane.showMessageDialog(null, "An ox has died!");
			break;

		//Ox is tired
		case 8:
			Wagon.setPace(Pace.Steady);
			JOptionPane.showMessageDialog(null, "The oxen are tired! You must move at a steady pace.");
			break;

		//Hunting Party adds Food
		case 9:
			inventoryController.getWagonInventory().get(1).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()+4);
			JOptionPane.showMessageDialog(null, "A hunting party has noticed you are short on food. 20 pounds of food has been added to your wagon!");
			break;

		//Wagon Crash
		case 10:
			switch (randNum6) {
			case 0:
				if (inventoryController.getWagonInventory().get(0).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(0).setQuantity(inventoryController.getWagonInventory().get(0).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! An ox was lost from your wagon!");
				}
				break;
			case 1:
				if (inventoryController.getWagonInventory().get(1).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(1).setQuantity(inventoryController.getWagonInventory().get(1).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! Food was lost from your wagon!");
				}
				break;
			case 2:
				if (inventoryController.getWagonInventory().get(2).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(2).setQuantity(inventoryController.getWagonInventory().get(2).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! Clothing was lost from your wagon!");
				}
				break;
			case 3:
				if (inventoryController.getWagonInventory().get(3).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(3).setQuantity(inventoryController.getWagonInventory().get(3).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! A box of ammo was lost from your wagon!");
				}
				break;
			case 4:
				if (inventoryController.getWagonInventory().get(4).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(4).setQuantity(inventoryController.getWagonInventory().get(4).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! A wheel was lost from your wagon!");
				}
				break;
			case 5:
				if (inventoryController.getWagonInventory().get(5).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(5).setQuantity(inventoryController.getWagonInventory().get(5).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! An axle was lost from your wagon!");
				}
				break;
			case 6:
				if (inventoryController.getWagonInventory().get(6).getQuantity() > 0) {
					inventoryController.getWagonInventory().get(6).setQuantity(inventoryController.getWagonInventory().get(6).getQuantity()-1);
					JOptionPane.showMessageDialog(null, "The wagon has crashed! A tongue was lost from your wagon!");
				}
				break;
			}
		break;
		}

	}

}